<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 17/2/24
 * Time: 下午5:59
 */
//require_once(dirname(__FILE__) . '/android/AndroidBroadcast.php');
//require_once(dirname(__FILE__) . '/android/AndroidFilecast.php');
//require_once(dirname(__FILE__) . '/android/AndroidGroupcast.php');
//require_once(dirname(__FILE__) . '/android/AndroidUnicast.php');
//require_once(dirname(__FILE__) . '/android/AndroidCustomizedcast.php');
//require_once(dirname(__FILE__) . '/android/AndroidListcast.php');
require_once(dirname(__FILE__) . '/ios/IOSBroadcast.php');
require_once(dirname(__FILE__) . '/ios/IOSFilecast.php');
require_once(dirname(__FILE__) . '/ios/IOSGroupcast.php');
require_once(dirname(__FILE__) . '/ios/IOSUnicast.php');
require_once(dirname(__FILE__) . '/ios/IOSCustomizedcast.php');
require_once(dirname(__FILE__) . '/ios/IOSListcast.php');

class UPush {
    protected $app_key  = NULL;
    protected $app_master_secret  = NULL;
    protected $timestamp        = NULL;
    protected $validation_token = NULL;

    function __construct($app_key,$app_master_secret) {
        $this->app_key = $app_key;
        $this->app_master_secret = $app_master_secret;
        $this->timestamp = strval(time());
    }


    function sendAndroidBroadcast($msg_body, $extra) {
        try {
            $brocast = new AndroidBroadcast();
            $brocast->setAppMasterSecret($this->app_master_secret);
            $brocast->setPredefinedKeyValue("appkey", $this->app_key);
            $brocast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            foreach($msg_body as $key => $value){
                $brocast->setPredefinedKeyValue($key, $value);
            }
            foreach($extra as $key => $value){
                $brocast->setExtraField($key, $value);
            }
            $ret = $brocast->send();
            return self::initResult($ret);
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidListcast($msg_body, $device_token_str, $extra) {
        try {
            $listcast = new AndroidListcast();
            $listcast->setAppMasterSecret($this->app_master_secret);
            $listcast->setPredefinedKeyValue("appkey",           $this->app_key);
            $listcast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            foreach($msg_body as $key => $value){
                $listcast->setPredefinedKeyValue($key, $value);
            }
            foreach($extra as $key => $value){
                $listcast->setExtraField($key, $value);
            }
            // Set your alias_type here
            $listcast->setPredefinedKeyValue("device_tokens",    $device_token_str);
            $ret = $listcast->send();
            return self::initResult($ret);
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidUnicast() {
        try {
            $unicast = new AndroidUnicast();
            $unicast->setAppMasterSecret($this->app_master_secret);
            $unicast->setPredefinedKeyValue("appkey",           $this->app_key);
            $unicast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            // Set your device tokens here
            $unicast->setPredefinedKeyValue("device_tokens",    "xx");
            $unicast->setPredefinedKeyValue("ticker",           "Android unicast ticker");
            $unicast->setPredefinedKeyValue("title",            "Android unicast title");
            $unicast->setPredefinedKeyValue("text",             "Android unicast text");
            $unicast->setPredefinedKeyValue("after_open",       "go_app");
            // Set 'production_mode' to 'false' if it's a test device.
            // For how to register a test device, please see the developer doc.
            $unicast->setPredefinedKeyValue("production_mode", "true");
            // Set extra fields
            $unicast->setExtraField("test", "helloworld");
//            print("Sending unicast notification, please wait...\r\n");
            $unicast->send();
//            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidFilecast($msg_body, $device_token_str, $extra) {
        try {
            $filecast = new AndroidFilecast();
            $filecast->setAppMasterSecret($this->app_master_secret);
            $filecast->setPredefinedKeyValue("appkey",           $this->app_key);
            $filecast->setPredefinedKeyValue("timestamp",        $this->timestamp);

            foreach($msg_body as $key => $value){
                $filecast->setPredefinedKeyValue($key, $value);
            }
            foreach( $extra as $key => $value ){
                $filecast->setExtraField($key, $value);
            }

            // Upload your device tokens, and use '\n' to split them if there are multiple tokens
            $filecast->uploadContents($device_token_str);
            $ret = $filecast->send();
            return self::initResult($ret);
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidGroupcast() {
        try {
            $filter = 	array(
                "where" => 	array(
                    "and" 	=>  array(
                        array(
                            "tag" => "test"
                        ),
                        array(
                            "tag" => "Test"
                        )
                    )
                )
            );

            $groupcast = new AndroidGroupcast();
            $groupcast->setAppMasterSecret($this->app_master_secret);
            $groupcast->setPredefinedKeyValue("appkey",           $this->app_key);
            $groupcast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            // Set the filter condition
            $groupcast->setPredefinedKeyValue("filter",           $filter);
            $groupcast->setPredefinedKeyValue("ticker",           "Android groupcast ticker");
            $groupcast->setPredefinedKeyValue("title",            "Android groupcast title");
            $groupcast->setPredefinedKeyValue("text",             "Android groupcast text");
            $groupcast->setPredefinedKeyValue("after_open",       "go_app");
            // Set 'production_mode' to 'false' if it's a test device.
            // For how to register a test device, please see the developer doc.
            $groupcast->setPredefinedKeyValue("production_mode", "true");
//            print("Sending groupcast notification, please wait...\r\n");
            $groupcast->send();
//            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidCustomizedcast() {
        try {
            $customizedcast = new AndroidCustomizedcast();
            $customizedcast->setAppMasterSecret($this->app_master_secret);
            $customizedcast->setPredefinedKeyValue("appkey",           $this->app_key);
            $customizedcast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            // Set your alias here, and use comma to split them if there are multiple alias.
            // And if you have many alias, you can also upload a file containing these alias, then
            // use file_id to send customized notification.
            $customizedcast->setPredefinedKeyValue("alias",            "xx");
            // Set your alias_type here
            $customizedcast->setPredefinedKeyValue("alias_type",       "xx");
            $customizedcast->setPredefinedKeyValue("ticker",           "Android customizedcast ticker");
            $customizedcast->setPredefinedKeyValue("title",            "Android customizedcast title");
            $customizedcast->setPredefinedKeyValue("text",             "Android customizedcast text");
            $customizedcast->setPredefinedKeyValue("after_open",       "go_app");
//            print("Sending customizedcast notification, please wait...\r\n");
            $customizedcast->send();
//            return("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendAndroidCustomizedcastFileId() {
        try {
            $customizedcast = new AndroidCustomizedcast();
            $customizedcast->setAppMasterSecret($this->app_master_secret);
            $customizedcast->setPredefinedKeyValue("appkey",           $this->app_key);
            $customizedcast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            // if you have many alias, you can also upload a file containing these alias, then
            // use file_id to send customized notification.
            $customizedcast->uploadContents("aa"."\n"."bb");
            // Set your alias_type here
            $customizedcast->setPredefinedKeyValue("alias_type",       "xx");
            $customizedcast->setPredefinedKeyValue("ticker",           "Android customizedcast ticker");
            $customizedcast->setPredefinedKeyValue("title",            "Android customizedcast title");
            $customizedcast->setPredefinedKeyValue("text",             "Android customizedcast text");
            $customizedcast->setPredefinedKeyValue("after_open",       "go_app");
            return ("Sending customizedcast notification, please wait...\r\n");
            $customizedcast->send();
            return ("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }



    function sendIOSBroadcast($msg_body, $extra) {
        try {
            $brocast = new IOSBroadcast();
            $brocast->setAppMasterSecret($this->app_master_secret);
            $brocast->setPredefinedKeyValue("appkey",           $this->app_key);
            $brocast->setPredefinedKeyValue("timestamp",        $this->timestamp);

            foreach($msg_body as $key => $value){
                $brocast->setPredefinedKeyValue($key, $value);
            }
            foreach($extra as $key => $value){
                $brocast->setCustomizedField($key, $value);
            }
            $ret = $brocast->send();
            return self::initResult($ret);
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendIOSListcast($msg_body, $device_token_str, $extra) {
        try {
            $listcast = new IOSListcast();
            $listcast->setAppMasterSecret($this->app_master_secret);
            $listcast->setPredefinedKeyValue("appkey",           $this->app_key);
            $listcast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            $listcast->setPredefinedKeyValue("device_tokens", $device_token_str);

            foreach($msg_body as $key => $value){
                $listcast->setPredefinedKeyValue($key, $value);
            }

            foreach( $extra as $key => $value ){
                $listcast->setCustomizedField($key, $value);
            }

            $ret = $listcast->send();
            return self::initResult($ret);
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    // $device_token_str 用\n分割的tokens
    function sendIOSFilecast($msg_body, $device_token_str, $extra) {
        try {
            $filecast = new IOSFilecast();
            $filecast->setAppMasterSecret($this->app_master_secret);
            $filecast->setPredefinedKeyValue("appkey",           $this->app_key);
            $filecast->setPredefinedKeyValue("timestamp",        $this->timestamp);

            foreach($msg_body as $key => $value){
                $filecast->setPredefinedKeyValue($key, $value);
            }
            foreach( $extra as $key => $value ){
                $filecast->setCustomizedField($key, $value);
            }

            // Upload your device tokens, and use '\n' to split them if there are multiple tokens
            $filecast->uploadContents($device_token_str);
            $ret = $filecast->send();
            return self::initResult($ret);
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendIOSUnicast() {
        try {
            $unicast = new IOSUnicast();
            $unicast->setAppMasterSecret($this->app_master_secret);
            $unicast->setPredefinedKeyValue("appkey",           $this->app_key);
            $unicast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            // Set your device tokens here
            $unicast->setPredefinedKeyValue("device_tokens",    "xx");
            $unicast->setPredefinedKeyValue("alert", "IOS 单播测试");
            $unicast->setPredefinedKeyValue("badge", 0);
            $unicast->setPredefinedKeyValue("sound", "chime");
            // Set 'production_mode' to 'true' if your app is under production mode
            $unicast->setPredefinedKeyValue("production_mode", "false");
            // Set customized fields
            $unicast->setCustomizedField("test", "helloworld");
//            print("Sending unicast notification, please wait...\r\n");
            $unicast->send();
//            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendIOSGroupcast() {
        try {
            /*
              *  Construct the filter condition:
              *  "where":
              *	{
              *		"and":
              *		[
                *			{"tag":"iostest"}
              *		]
              *	}
              */
            $filter = 	array(
                "where" => 	array(
                    "and" 	=>  array(
                        array(
                            "tag" => "iostest"
                        )
                    )
                )
            );

            $groupcast = new IOSGroupcast();
            $groupcast->setAppMasterSecret($this->app_master_secret);
            $groupcast->setPredefinedKeyValue("appkey",           $this->app_key);
            $groupcast->setPredefinedKeyValue("timestamp",        $this->timestamp);
            // Set the filter condition
            $groupcast->setPredefinedKeyValue("filter",           $filter);
            $groupcast->setPredefinedKeyValue("alert", "IOS 组播测试");
            $groupcast->setPredefinedKeyValue("badge", 0);
            $groupcast->setPredefinedKeyValue("sound", "chime");
            // Set 'production_mode' to 'true' if your app is under production mode
            $groupcast->setPredefinedKeyValue("production_mode", "false");
//            return ("Sending groupcast notification, please wait...\r\n");
            $groupcast->send();
//            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    function sendIOSCustomizedcast() {
        try {
            $customizedcast = new IOSCustomizedcast();
            $customizedcast->setAppMasterSecret($this->app_master_secret);
            $customizedcast->setPredefinedKeyValue("appkey",           $this->app_key);
            $customizedcast->setPredefinedKeyValue("timestamp",        $this->timestamp);

            // Set your alias here, and use comma to split them if there are multiple alias.
            // And if you have many alias, you can also upload a file containing these alias, then
            // use file_id to send customized notification.
            $customizedcast->setPredefinedKeyValue("alias", "xx");
            // Set your alias_type here
            $customizedcast->setPredefinedKeyValue("alias_type", "xx");
            $customizedcast->setPredefinedKeyValue("alert", "IOS 个性化测试");
            $customizedcast->setPredefinedKeyValue("badge", 0);
            $customizedcast->setPredefinedKeyValue("sound", "chime");
            // Set 'production_mode' to 'true' if your app is under production mode
            $customizedcast->setPredefinedKeyValue("production_mode", "false");
//            print("Sending customizedcast notification, please wait...\r\n");
            $customizedcast->send();
//            print("Sent SUCCESS\r\n");
        } catch (Exception $e) {
            return ("Caught exception: " . $e->getMessage());
        }
    }

    static protected function initResult($ret_json){
        $arr = @json_decode($ret_json, true);
        if( empty($arr) ) return array(false, '返回json解析失败，或者为空', array());
        if( strtoupper($arr['ret']) !== 'SUCCESS' ) return array(false, $arr['data']['error_code'], $arr['data']);
        return array(true, '', $arr['data']);
    }


}